'''
Author: thinktanker thinktanker@163.com
Date: 2024-06-22 22:01:07
LastEditors: thinktanker thinktanker@163.com
LastEditTime: 2024-06-22 22:15:24
FilePath: 
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
'''
从这里启动可以保证是管理员权限启动
'''
import subprocess
import platform
# import os
import ctypes
import sys


def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False


def run_as_admin(command):
    if not is_admin():
        print("开启管理员权限")
        params = ' '.join(command)
        ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable,
                                            params, None, 1)
    print("管理员权限已开启")
    result = subprocess.run(command, capture_output=True, text=True)

    # 输出结果
    print("Command:", ' '.join(command))
    print("Return Code:", result.returncode)
    print("Standard Output:")
    print(result.stdout)
    print("Standard Error:")
    print(result.stderr)


def check_windows_version():
    release_info = platform.version()
    release_info = release_info.split(".")[-1]  # 去除可能存在的额外信息
    # print(release_info)

    # Windows 10 版本号通常包含 "10.", 而 Windows 11 版本号则有 "10." 也可能直接是 "21H2" 或其他标识
    if int(release_info) > 21000:
        return "win11"
    else:
        return "win10"


bat_file_path = '0win11_run.bat'
if check_windows_version() == 'win10':
    # 批处理文件路径
    bat_file_path = '0win10_run.bat'
# else:
#     bat_file_path = './config/00.bat'
# 构建命令
cmd = [
    'powershell', '-Command', f'Start-Process -Verb runas "{bat_file_path}"'
]

# try:
#     subprocess.run(cmd, check=True)
# except subprocess.CalledProcessError as e:
#     print(f"Error: {e}")

run_as_admin(cmd)
